<template>
  <div class="login-container">
    <div class="login-header">
      <img src="https://s02.mifile.cn/assets/static/image/mi-logo.png" alt="小米logo" class="logo">
      <h1>小米账号登录</h1>
    </div>

    <div class="login-form">
      <form @submit.prevent="handleSubmit">
        <div class="form-group">
          <input type="text" v-model="username" placeholder="请输入邮箱/手机号码/小米ID" required>
        </div>

        <div class="form-group password-group">
          <input :type="showPassword ? 'text' : 'password'" v-model="pwd" placeholder="请输入密码" required>
          <span class="toggle-password" @click="togglePassword">
            <i :class="showPassword ? 'iconfont icon-eye' : 'iconfont icon-eye-close'"></i>
          </span>
        </div>


        <div class="form-options">

          <div class="agree-terms">
            <input type="checkbox" v-model="agree" required /> 我已阅读并同意 <a href="#">用户协议</a> 和 <a href="#">隐私政策</a>
          </div>
          <router-link to="/forgot-password" class="forgot-password">忘记密码？</router-link>
        </div>

        <button type="submit" class="login-btn">登录</button>

        <div class="other-options">
          <router-link to="/register" class="register-link">手机号注册</router-link>
          <span>|</span>
          <a href="#" class="scan-login">扫码登录</a>
        </div>
      </form>

      <div class="third-party-login">
        <p>其他方式登录</p>
        <div class="third-party-icons">
          <a href="#" class="icon-weibo"><i class="iconfont icon-weibo">微博</i></a>
          <a href="#" class="icon-wechat"><i class="iconfont icon-wechat">微信</i></a>
          <a href="#" class="icon-qq"><i class="iconfont icon-qq">QQ</i></a>
          <a href="#" class="icon-alipay"><i class="iconfont icon-alipay">支付宝</i></a>
        </div>
      </div>
    </div>

    <div class="login-footer">
      <p>简体 | 繁体 | English | 常见问题 | 隐私政策</p>
      <p>小米公司版权所有-京ICP备10046444-京公网安备11010802020134号-京ICP证110507号</p>
    </div>
  </div>
</template>

<script setup>
import axios from '../api/axios'
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const username = ref('')
const pwd = ref('')
const agree = ref(false)
const showPassword = ref(false) // 新增状态控制密码显示/隐藏
const router = useRouter()
// 新增切换密码显示/隐藏的方法
const togglePassword = () => {
  showPassword.value = !showPassword.value
}
const handleSubmit = () => {
  if (!agree.value) {
    alert("您必须同意用户协议和隐私政策才能登录");
    return;
  }

  axios({
    url: '/api/users/login', // 服务端接口地址
    method: 'post', // 请求方法：查询：get  添加：post 删除：delete
    data: {
      username: username.value,
      pwd: pwd.value
    }
  }).then((res) => {
    if (res.data.code === 200) {
      sessionStorage.setItem('token', res.data.token);
      const user = res.data.user;
      sessionStorage.setItem('user', JSON.stringify(user));

      // 根据用户角色跳转不同的页面
      if (user.role === 'root') {
        router.push('/admin');
      } else if (user.role === 'user') {
        router.push('/home');
      } else {
        alert("未知角色，请联系管理员");
        router.push('/');
      }
      setTimeout(() => {
        location.reload();
      }, 1000);
    } else {
      alert(res.data.msg)
    }
  }).catch((err) => {
    console.error('登录失败:', err);
    alert("登录失败，请重试");
  });
}
</script>

<style scoped>
.login-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background-color: #f5f5f5;
  font-family: "Helvetica Neue", Helvetica, Arial, "Microsoft Yahei", sans-serif;
}

.login-header {
  display: flex;
  /* 启用Flexbox布局 */
  align-items: center;
  /* 垂直居中对齐子元素 */
  justify-content: center;
  /* 水平居中对齐子元素 */
  margin-bottom: 30px;
  text-align: left;
  /* 由于现在是flex布局，需要重新设置文本对齐方式 */
}

.logo {
  margin-bottom: 0;
  /* 移除底部外边距，因为现在不需要了 */
  margin-right: 50px;
  /* 在logo和h1之间增加右边距 */
}

.login-header h1 {
  font-size: 28px;
  font-weight: normal;
  color: #333;
  white-space: nowrap;
  /* 防止文字换行 */
}

.login-form {
  width: 350px;
  padding: 40px;
  background-color: #fff;
  box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
  border-radius: 8px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group input {
  width: 100%;
  height: 48px;
  padding: 0 16px;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  font-size: 16px;
  outline: none;
  transition: border-color 0.3s ease;
  box-sizing: border-box;
}

.form-group input:focus {
  border-color: #ff6700;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  font-size: 14px;
  color: #757575;
}

.agree-terms a {
  color: #ff6700;
  text-decoration: none;
}

.agree-terms a:hover {
  text-decoration: underline;
}

.forgot-password {
  color: #757575;
  text-decoration: none;
}

.forgot-password:hover {
  color: #ff6700;
}

.login-btn {
  width: 100%;
  height: 48px;
  background-color: #ff6700;
  color: #fff;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s ease;
  margin-bottom: 20px;
}

.login-btn:hover {
  background-color: #f25807;
}

.other-options {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
  margin-bottom: 30px;
  font-size: 14px;
  color: #757575;
}

.register-link,
.scan-login {
  color: #757575;
  text-decoration: none;
}

.register-link:hover,
.scan-login:hover {
  color: #ff6700;
}

.third-party-login {
  text-align: center;
  border-top: 1px solid #e0e0e0;
  padding-top: 20px;
  margin-bottom: 30px;
}

.third-party-login p {
  color: #b0b0b0;
  font-size: 14px;
  margin-bottom: 10px;
}

.third-party-icons {
  display: flex;
  justify-content: center;
  gap: 20px;
}

.third-party-icons a {
  color: #757575;
  text-decoration: none;
  font-size: 16px;
  transition: color 0.3s ease;
}

.third-party-icons a:hover {
  color: #ff6700;
}

.login-footer {
  margin-top: 60px;
  text-align: center;
  font-size: 12px;
  color: #b0b0b0;
}

.login-footer p {
  margin: 5px 0;
}

.password-group {
  position: relative;
}

.toggle-password {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  color: #757575;
}

.toggle-password:hover {
  color: #ff6700;
}

/* 如果你没有引入iconfont，可以使用以下样式作为替代 */
.iconfont {
  display: inline-block;
  width: 20px;
  height: 20px;
  background-size: contain;
  background-repeat: no-repeat;
}

.icon-eye {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%23757575'%3E%3Cpath d='M12 4.5C7 4.5 2.73 7.61 1 12c1.73 4.39 6 7.5 11 7.5s9.27-3.11 11-7.5c-1.73-4.39-6-7.5-11-7.5zM12 17c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5 5-2.24 5-5 5zm0-8c-1.66 0-3 1.34-3 3s1.34 3 3 3 3-1.34 3-3-1.34-3-3-3z'/%3E%3C/svg%3E");
}

.icon-eye-close {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%23757575'%3E%3Cpath d='M12 7c2.76 0 5 2.24 5 5 0 .65-.13 1.26-.36 1.83l2.92 2.92c1.51-1.26 2.7-2.89 3.43-4.75-1.73-4.39-6-7.5-11-7.5-1.4 0-2.74.25-3.98.7l2.16 2.16C10.74 7.13 11.35 7 12 7zM2 4.27l2.28 2.28.46.46C3.08 8.3 1.78 10.02 1 12c1.73 4.39 6 7.5 11 7.5 1.55 0 3.03-.3 4.38-.84l.42.42L19.73 22 21 20.73 3.27 3 2 4.27zM7.53 9.8l1.55 1.55c-.05.21-.08.43-.08.65 0 1.66 1.34 3 3 3 .22 0 .44-.03.65-.08l1.55 1.55c-.67.33-1.41.53-2.2.53-2.76 0-5-2.24-5-5 0-.79.2-1.53.53-2.2zm4.31-.78l3.15 3.15.02-.16c0-1.66-1.34-3-3-3l-.17.01z'/%3E%3C/svg%3E");
}

/* 调整密码输入框的padding，为小眼睛留出空间 */
.password-group input {
  padding-right: 40px !important;
}

.third-party-icons {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-top: 15px;
}

.third-party-icons a {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: #f5f5f5;
  transition: all 0.3s ease;
  text-decoration: none;
  color: transparent;
  /* 隐藏文字 */
}

.third-party-icons a:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}



.icon-weibo:hover {
  background-color: #E6162D;
}

.icon-wechat:hover {
  background-color: #07C160;
}

.icon-qq:hover {
  background-color: #12B7F5;
}

.icon-alipay:hover {
  background-color: #007AFF;
}

.third-party-icons a:hover i {
  filter: brightness(0) invert(1);
}
</style>
